// 渲染进程
const { BrowserWindow, getCurrentWindow } = require("@electron/remote");
window.addEventListener("DOMContentLoaded", () => {
  // 点击按钮打开新窗口
  const pBtn = document.querySelector(".p-btn");
  pBtn.addEventListener("click", () => {
    // 如何创建新窗口
    let indexWin = new BrowserWindow({
      width: 800,
      height: 600
    });
    indexWin.loadFile("./index.html");
  });

  const [closeBtn, maxBtn, minBtn] = document.querySelectorAll(".oper-box li");

  // 阻止关闭
  window.onbeforeunload = () => {
    let [confirmBtn, cancelBtn] = document.querySelectorAll(".mask-oper-box span");
    let maskBox = document.querySelector(".mask-box");
    maskBox.style.display = 'block';
    confirmBtn.addEventListener("click", () => {
      maskBox.style.display = 'none';
      // 销毁实例
      currentWindow.destroy();
    });
    cancelBtn.addEventListener("click", () => {
      maskBox.style.display = 'none';
    });
    return false;
  };
  
  const currentWindow = getCurrentWindow();
  closeBtn.addEventListener("click", () => {
    currentWindow.close();
  });
  maxBtn.addEventListener("click", () => {
    // 是否最大化 ？ 恢复原状态 ： 最大化窗口 ；
    currentWindow.isMaximized() ? currentWindow.restore() : currentWindow.maximize();
  });
  minBtn.addEventListener("click", () => {
    currentWindow.isMinimized() ? currentWindow.restore() : currentWindow.minimize();
  });
});